from src.vobjects.video import Video
from src.vobjects.item import Item

from src.config import Config
from src.constants import *
from src.utils import *

import urllib.request
import os

class Torrent:

    def __init__(self, title: str="", url: str="", is_video: bool=False, is_file: bool=False):
        self.title      = title
        self.url        = url
        self.is_video   = is_video
        self.is_file    = is_file

    def to_file(self):
        urllib.request.urlretrieve(self.url, f"{self.title}.torrent")

    def play(self):
        if self.is_video:
            os.system(f"webtorrent '{self.url}' --mpv")

    def to_item(self):
        status = {
            "url" : self.url
        }

        return Item(self.title, "", "", "", self, subtitle="", status=status)

    @staticmethod
    def from_json(jsn): 
        return Torrent(
            title       = jsn['title'],
            url         = jsn['url'],
            is_video    = jsn['is_video'],
        )




